package com.laicunba.ext.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.stereotype.Service;

import com.laicunba.ext.service.SlaveActivityJoinService;

@Service
public class SlaveActivityJoinServiceImpl extends BaseService implements SlaveActivityJoinService {

	@Override
	public Map<String, Object> findTotalReward(String activityId, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("userId", userId);
		return slaveGenericQueryDao.selectOne("SLAVE_ACTIVITY_JOIN.findTotalReward", map);
	}
	
	@Override
	public int findGainCount(String activityId, String userId, String beginDate, String endDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("userId", userId);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		return slaveGenericQueryDao.selectOne("SLAVE_ACTIVITY_JOIN.findGainCount", map);
	}
	
	@Override
	public List<Map<String, Object>> findActivityJoinList(String activityId, String subActId, String userId, int limit) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("subActId", subActId);
		map.put("userId", userId);
		map.put("limit", limit);
		return slaveGenericQueryDao.selectList("SLAVE_ACTIVITY_JOIN.findActivityJoinList", map);
	}
	
	@Override
	public List<Map<String, Object>> findUserRewardData(String activityId, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("userId", userId);
		return slaveGenericQueryDao.selectList("SLAVE_ACTIVITY_JOIN.findUserRewardData", map);
	}
	
	@Override
	public List<Map<String, Object>> analysisUserReward(String activityId, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("userId", userId);
		return slaveGenericQueryDao.selectList("SLAVE_ACTIVITY_JOIN.analysisUserReward", map);
	}
	
	@Override
	public List<Map<String, Object>> findActivityUserRewardList(String activityId, String userId) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("userId", userId);
		List<Map<String, Object>> rewardList = slaveGenericQueryDao.selectList("SLAVE_ACTIVITY_JOIN.findActivityUserRewardList", map);
		for (Map<String, Object> rewardMap : rewardList) {
			if (rewardMap.get("summary") != null) {
				Map<String, Object> summaryMap = (Map<String, Object>) new JSONParser().parse(rewardMap.get("summary").toString());
				if (summaryMap != null && !summaryMap.isEmpty()) {
					rewardMap.putAll(summaryMap);
				}
			}
		}
		return rewardList;
	}
	
	@Override
	public List<Map<String, Object>> findUserRewardList(String activityId, String subActivityId, String userId, Integer limitNums) throws ParseException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("activityId", activityId);
		map.put("subActivityId", subActivityId);
		map.put("userId", userId);
		map.put("limitNums", limitNums);
		
		return slaveGenericQueryDao.selectList("SLAVE_ACTIVITY_JOIN.findActivityUserRewardList", map);
	}
	
}
